//
//  CallLogController.m
//  HandyDialer
//
//	Developed by Team Synergy
//  Created by Jordan Tseng on 11/14/11.
//  Coders: Jacky Li and Jordan Tseng 
//  Graphic Designer: Derek Fong
//  Developers: Brad Lisson, Derek Fong, Jacky Li, Jeremy Gangoso, Jordan Tseng
//  Copyright HandyDialer 2011. All rights reserved.

#import "MenuViewController.h"
#import "CallLogViewController.h"


@implementation CallLogViewController
@synthesize highlightArea;
@synthesize backgroundArea;
@synthesize num1, num2, num3, num4;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}



#pragma mark - View lifecycle


- (void)dealloc {
	[highlightArea release];
    [super dealloc];
}

- (void)viewDidLoad {
    
    row = 0;
    defaults = [NSUserDefaults standardUserDefaults];
    callLogEntries = [defaults mutableArrayValueForKey:@"CallLog"];
	highlightColor = [defaults stringForKey:@"Highlight"];
	speed = [defaults floatForKey:@"Speed"];
	themeColor = [defaults stringForKey:@"Theme"];
	// special case for white theme to make text easier to read
	if ([themeColor isEqual:@"white"])
	{
		num1.textColor = [UIColor blackColor];
		num2.textColor = [UIColor blackColor];
		num3.textColor = [UIColor blackColor];
		num4.textColor = [UIColor blackColor];
	}
    [backgroundArea setImage:[UIImage imageNamed: [themeColor stringByAppendingString:@"_log_default.png"]]];
	[super viewDidLoad];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    defaults = [NSUserDefaults standardUserDefaults];
    callLogEntries = [defaults mutableArrayValueForKey:@"CallLog"];
	highlightColor = [defaults stringForKey:@"Highlight"];
	speed = [defaults floatForKey:@"Speed"];
	themeColor = [defaults stringForKey:@"Theme"];
    [backgroundArea setImage:[UIImage imageNamed: [themeColor stringByAppendingString:@"_log_default.png"]]];
    int numOfCallLogEntries = [callLogEntries count];
	NSLog(@"%d",numOfCallLogEntries);
    switch(numOfCallLogEntries)
    {
        case 1:
            num1.text = [callLogEntries objectAtIndex:0];
            break;
        case 2:
            num1.text = [callLogEntries objectAtIndex:0];
            num2.text = [callLogEntries objectAtIndex:1];
            break;
        case 3:
            num1.text = [callLogEntries objectAtIndex:0];
            num2.text = [callLogEntries objectAtIndex:1];
            num3.text = [callLogEntries objectAtIndex:2];
            break;
        case 4:
            num1.text = [callLogEntries objectAtIndex:0];
            num2.text = [callLogEntries objectAtIndex:1];
            num3.text = [callLogEntries objectAtIndex:2];
            num4.text = [callLogEntries objectAtIndex:3];
        default:
            break;
    }
    
	if (uniTimer == nil)
		uniTimer = [NSTimer scheduledTimerWithTimeInterval:(speed) target:self selector:@selector(optionChange) userInfo:nil repeats:YES];
	[super viewDidAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[uniTimer invalidate];
	uniTimer = nil;
	[super viewDidDisappear:animated];
}


- (IBAction)callNumber:(id)sender
{
	callLogEntries = [defaults mutableArrayValueForKey:@"CallLog"];
    switch(row)
    {
        case 1:
        {
            if ([callLogEntries count] >= 1)
            {
                [[UIApplication sharedApplication] openURL:[[NSURL alloc] initWithString:[NSString stringWithFormat:@"tel:%@", [callLogEntries objectAtIndex:0]]]];
                [self dismissModalViewControllerAnimated:NO];
            }
            break;
        }
        case 2:
        {
            if ([callLogEntries count] >= 2)
            {
				NSString *calledNumber = [callLogEntries objectAtIndex:1];
				[callLogEntries removeObjectAtIndex:1];
				[callLogEntries insertObject:calledNumber atIndex:0];
                [[UIApplication sharedApplication] openURL:[[NSURL alloc] initWithString:[NSString stringWithFormat:@"tel:%@", calledNumber]]];
				[defaults setObject:callLogEntries forKey:@"CallLog"];
				[defaults synchronize];
                [self dismissModalViewControllerAnimated:NO];
            }
            break;
        }
        case 3:
        {
            if ([callLogEntries count] >= 3)
            {
				NSString *calledNumber = [callLogEntries objectAtIndex:2];
				[callLogEntries removeObjectAtIndex:2];
				[callLogEntries insertObject:calledNumber atIndex:0];
                [[UIApplication sharedApplication] openURL:[[NSURL alloc] initWithString:[NSString stringWithFormat:@"tel:%@", calledNumber]]];
				[defaults setObject:callLogEntries forKey:@"CallLog"];
				[defaults synchronize];
                [self dismissModalViewControllerAnimated:NO];
            }
            break;
        }
        case 4:
        {
            if ([callLogEntries count] >= 4)
            {
				NSString *calledNumber = [callLogEntries objectAtIndex:3];
				[callLogEntries removeObjectAtIndex:3];
				[callLogEntries insertObject:calledNumber atIndex:0];
                [[UIApplication sharedApplication] openURL:[[NSURL alloc] initWithString:[NSString stringWithFormat:@"tel:%@", calledNumber]]];
				[defaults setObject:callLogEntries forKey:@"CallLog"];
				[defaults synchronize];
                [self dismissModalViewControllerAnimated:NO];
            }
            break;
        }
        case 5:
		{
            [self dismissModalViewControllerAnimated:NO];
            break;
		}
        default:
		{
            break;
		}
    }
}



- (void)highlightChange:(NSString *)picture
{
    UIImage *image = [UIImage imageNamed: picture];
    [highlightArea setImage:image];
	
}

// Decides which screen to go to
-(void)optionChange 
{
    
    if (row == 5)
    {
        row = 0;
    }
    row += 1;
    NSString *next = highlightColor;
	next = [next stringByAppendingString:@"_log_"];  
	next = [next stringByAppendingString:[NSString stringWithFormat:@"%d", row]];
	next = [next stringByAppendingString:@".png"];
	[self highlightChange:next];
}

@end
